package udp_client;

import java.awt.*;
import java.util.*;
import javax.media.Buffer;
import javax.media.Format;
import javax.media.Manager;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.*;
import java.awt.event.*;

public class myCamera implements Runnable{
	public void run(){
		VideoFrame frame= new VideoFrame();
		for  (int i=0; i<1000;i++){
			frame.update_image();
			try {
				Thread.sleep(25);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		frame.player.close();
		System.out.println("player quit");
	}
}
class VideoFrame extends JFrame{
	private static final long serialVersionUID = 1L;
	private Vector<javax.media.CaptureDeviceInfo>  deviceList=javax.media.CaptureDeviceManager.getDeviceList(null);
    private javax.media.MediaLocator mediaLocator=null;
    public  Player player=null;
    private Buffer buffer=null;
	private BufferToImage bufferToImage=null;
    private Image image=null;
    private Component  comp;
    private FrameGrabbingControl fgc;
    public VideoFrame() {
        this.setTitle("myCamera");
        this.initCamera();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500,500);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
    
    public void initCamera(){
        javax.media.format.VideoFormat currentFormat = null;
        for(int i=0;i<deviceList.size();i++){
            System.out.println("device name: "+this.deviceList.get(i).getName());
            if(deviceList.get(i).getName().startsWith("vfw")){
                Format [] vedioFormat=deviceList.get(i).getFormats();
                for(int j=0;j<vedioFormat.length;i++){
                    if(vedioFormat[i] instanceof javax.media.format.VideoFormat){
                        currentFormat=(VideoFormat) vedioFormat[i];
                        break;
                    }
                }
                if(currentFormat==null){
                    System.out.println("this vedio device unavailable");
                }
                this.mediaLocator=deviceList.get(i).getLocator();
                System.out.println("the location of this vedio device:"+this.mediaLocator);
                this.createPlayerByMediaLocator();
                break;
            }
        }
    }
    
    public void createPlayerByMediaLocator(){
        try {
            System.out.println(this.mediaLocator);
            player   =  Manager.createRealizedPlayer(this.mediaLocator);
            player.start();
            comp=  player.getVisualComponent();
            if   ( comp !=   null) {
                this.getContentPane().add(comp, "Center");
                System.out.println("add comp to player");
            }
            comp.getGraphics();
            fgc=(FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
        } catch   (Exception   e) {
            e.printStackTrace();
        }
    }
    public void update_image(){
    	if   ( comp!=   null) {
      		buffer=fgc.grabFrame();
      		bufferToImage=new BufferToImage((VideoFormat)buffer.getFormat());
      		image=bufferToImage.createImage(buffer);
      		System.out.println(buffer.getLength());
      		if (buffer.getLength()>0){
      			if (ReadImage.get_myResource().size()<300){
      				ReadImage.get_myResource().add(image);
      			}
      		}
          }
    }
}